module.exports = {
  apps: [
    {
      // 应用基本配置
      name: 'snake-game',
      script: 'server.js',
      cwd: './',
      
      // 实例配置
      instances: 1, // 可以设置为 'max' 使用所有 CPU 核心
      exec_mode: 'fork', // 或 'cluster' 用于集群模式
      
      // 环境变量
      env: {
        NODE_ENV: 'development',
        PORT: 13000
      },
      env_production: {
        NODE_ENV: 'production',
        PORT: 13000
      },
      
      // 日志配置
      log_file: './logs/combined.log',
      out_file: './logs/out.log',
      error_file: './logs/error.log',
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      merge_logs: true,
      
      // 进程管理
      autorestart: true,
      watch: false, // 生产环境建议设为 false
      max_memory_restart: '1G',
      restart_delay: 4000,
      
      // 性能监控
      min_uptime: '10s',
      max_restarts: 10,
      
      // 进程行为
      kill_timeout: 5000,
      wait_ready: true,
      listen_timeout: 8000,
      
      // 高级配置
      node_args: '--max-old-space-size=1024',
      
      // 忽略的监听文件（当 watch: true 时）
      ignore_watch: [
        'node_modules',
        'logs',
        '*.log',
        '.git'
      ],
      
      // 监听的文件扩展名（当 watch: true 时）
      watch_options: {
        followSymlinks: false,
        usePolling: false
      }
    },
    
    // 开发环境配置
    {
      name: 'snake-game-dev',
      script: 'server.js',
      cwd: './',
      instances: 1,
      exec_mode: 'fork',
      
      env: {
        NODE_ENV: 'development',
        PORT: 1300
      },
      
      // 开发环境启用文件监听
      watch: true,
      ignore_watch: [
        'node_modules',
        'logs',
        '*.log',
        '.git',
        'README.md',
        'DEPLOYMENT.md'
      ],
      
      log_file: './logs/dev-combined.log',
      out_file: './logs/dev-out.log',
      error_file: './logs/dev-error.log',
      
      autorestart: true,
      max_memory_restart: '500M'
    }
  ],
  
  // 部署配置
  deploy: {
    production: {
      user: 'deploy',
      host: ['your-server.com'],
      ref: 'origin/main',
      repo: 'git@github.com:your-username/snake-game.git',
      path: '/var/www/snake-game',
      'pre-deploy-local': '',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': '',
      'ssh_options': 'StrictHostKeyChecking=no'
    },
    
    staging: {
      user: 'deploy',
      host: ['staging-server.com'],
      ref: 'origin/develop',
      repo: 'git@github.com:your-username/snake-game.git',
      path: '/var/www/snake-game-staging',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env staging',
      env: {
        NODE_ENV: 'staging',
        PORT: 3001
      }
    }
  }
};

/*
使用说明:

1. 生产环境启动:
   pm2 start ecosystem.config.js --env production

2. 开发环境启动:
   pm2 start ecosystem.config.js:snake-game-dev

3. 重启应用:
   pm2 restart snake-game

4. 停止应用:
   pm2 stop snake-game

5. 删除应用:
   pm2 delete snake-game

6. 查看日志:
   pm2 logs snake-game

7. 监控:
   pm2 monit

8. 保存 PM2 配置:
   pm2 save

9. 设置开机自启:
   pm2 startup
   pm2 save

10. 部署到生产环境:
    pm2 deploy production setup
    pm2 deploy production

11. 集群模式启动（多核 CPU）:
    修改 instances: 'max' 或具体数字
    修改 exec_mode: 'cluster'
*/